package com.qzdsoft.erpcloud.service.sale.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qzdsoft.erpcloud.domain.sale.SaleQuotationDetail;
import com.qzdsoft.erpcloud.mapper.proj.MaterialQueryMapper;
import com.qzdsoft.erpcloud.mapper.sale.SaleQuotationDetailMapper;
import com.qzdsoft.erpcloud.mapper.sale.vi.SaleQuotationDetailViEnMapper;
import com.qzdsoft.erpcloud.service.sale.SaleQuotationDetailService;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleOrderDetailEditItemInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailEditInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailQueryCriteriaViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailViEn;
import com.qzdsoft.utils.StringUtil;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

/**
 * 模块名：销售报价单明细
 * <p>详细描述：明细Server
 * @author DamingChen Sep 29, 2017
 * @see
 * @since 1.0
 */
@Service
public class SaleQuotationDetailServiceImpl implements SaleQuotationDetailService {

	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(SaleQuotationDetailServiceImpl.class);

	@Autowired
	private SaleQuotationDetailViEnMapper saleQuotationDetailViEnMapper;

	@Autowired
	private MaterialQueryMapper materialMapper;

	@Autowired
	private SaleQuotationDetailMapper saleQuotationDetailMapper;

	@Override
	public List<SaleQuotationDetailViEn> selectDetailBy(SaleQuotationDetailQueryCriteriaViEn info) {
		return saleQuotationDetailViEnMapper.selectById(info);
	}

	@Transactional
	@Override
	public Response<String> edit(SaleQuotationDetailEditInfo info) {
		logger.debug("编辑销售报价单详情提交数据为空{}", info);
		List<SaleOrderDetailEditItemInfo> details = info.getItems();
		if (CollectionUtils.isEmpty(details)) {
			logger.error("编辑销售报价单详情提交数据为空");
			return new Response<>(ResultEnum.ERROR);
		}
		List<SaleQuotationDetail> adddetails = new ArrayList<SaleQuotationDetail>();// 新增的销售报价单详情
		List<SaleQuotationDetail> editDetails = new ArrayList<SaleQuotationDetail>();// 修改的销售报价单详情
		for (SaleOrderDetailEditItemInfo d : details) {
			SaleQuotationDetail detail = new SaleQuotationDetail();
			if (!StringUtil.isEmpty(d.getId())) {
				detail = saleQuotationDetailMapper.selectByPrimaryKey(d.getId());
				if (detail == null) {
					logger.error("编辑销售报价单明细时,根据id,没找到相应的明细数据，可能改明细不存在", d.getId());
					throw new RuntimeException("编辑销售报价单明细时,根据id,没找到相应的明细数据，可能改明细不存在");
				}
				editDetails.add(detail);
			} else {
				adddetails.add(detail);
			}
			detail.setBillId(info.getBillId());
			detail.setBillNo(info.getBillNo());
			detail.setMaterialId(d.getMaterialId());
			
			MaterialInfo material = materialMapper.selectById(d.getMaterialId());
			if (material == null) {
				logger.error("物料id，根据物料id：{},没找到对应的物料信息");
				throw new RuntimeException("物料id错误");
			}
			// 物料有关设置
			detail.setMaterialCoding(material.getCode());
			detail.setMaterialSpec(material.getSpec());
			detail.setMaterialTypeId(material.getTypeid());
			detail.setMaterialName(material.getName());
			detail.setMeasurementUId(material.getMearsureUintid());
			detail.setcMaterialCoding(material.getAliasCode());
			detail.setMaterialAliasId(material.getAliasId());
			detail.setcMaterialName(material.getAliasName());
			detail.setcMaterialSpec(material.getAliasSpec());
			detail.setMaterialName(material.getName());
			detail.setBrandId(material.getBrandid());

			detail.setPrice(d.getPrice());
			detail.setAmount(d.getAmount());
		}
		int count = 0;
		if (!CollectionUtils.isEmpty(adddetails)) {
			count = saleQuotationDetailMapper.insertList(adddetails);
			if (count != adddetails.size()) {
				logger.error("编辑销售报价单明细错误，更新数据不对updatecount={},提交新增数据条数：{}", count, adddetails.size());
				throw new RuntimeException("编辑销售报价单明细错误，更新数据不对updatecount=" + count);
			}
		}

		count = 0;
		for (SaleQuotationDetail p : editDetails) {
			count += saleQuotationDetailMapper.updateByPrimaryKey(p);
		}
		if (count != editDetails.size()) {
			logger.error("编辑销售报价单明细错误，更新数据不对updatecount={},提交修改数据条数：{}", count, editDetails.size());
			throw new RuntimeException("编辑销售报价单明细错误，更新数据不对updatecount=" + count);
		}
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Override
	public Response<String> remove(List<Integer> ids) {
		int counts = saleQuotationDetailMapper.deleteByIds(ids);
		if (counts != ids.size()) {
			logger.error("编辑销售报价单明细错误，删除数据不对deletecount={},提交修改数据条数：{}", counts, ids.size());
			throw new RuntimeException("删除销售报价单明细错误，更新数据不对deletecount=" + counts);
		}
		return new Response<String>(ResultEnum.SUCCESS);
	}

}
